<template>
  <div>
    <!-- 表单开始 -->
    <el-form>
      <label class="el">实时查询：</label>
      <!-- 选择器开始 -->
      <el-select
        @change="search"
        clearable
        filterable
        class="el"
        v-model="statusValue"
        placeholder="选择或搜索状态"
      >
        <el-option
          v-for="item in status"
          :key="item"
          :label="item"
          :value="item"
        >
        </el-option>
      </el-select>
      <el-select
        @change="search"
        clearable
        filterable
        class="el"
        v-model="categoryValue"
        placeholder="选择或搜索栏目"
      >
        <el-option
          v-for="item in category"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <!-- 选择器结束 -->
      <!-- 查询标题开始 -->
      <el-input
        @input="search"
        class="el"
        v-model="title"
        placeholder="请输入标题"
      ></el-input>
      <!-- 查询标题结束 -->
      <!-- 按钮开始 -->
      <!-- <el-button type="primary" @click="search()">查询</el-button> -->
      <el-button
        style="float: right; margin: 10px"
        type="primary"
        @click="toPublish()"
        >发布风采</el-button
      >
      <!-- 按钮结束 -->
    </el-form>
    <!-- 表单结束 -->
    <!-- 表格开始 -->
    <el-table :data="info" style="width: 100%">
      <!-- 序号 -->
      <el-table-column width="80px" align="center" prop="id" label="序号">
      </el-table-column>
      <!-- 封面 -->
      <el-table-column width="150px" align="center" prop="cover" label="封面">
        <template slot-scope="scope">
          <img width="100px" :src="scope.row.cover" alt="" />
        </template>
      </el-table-column>
      <!-- 风采 -->
      <el-table-column prop="fengcai" label="风采" width="400px">
        <template slot-scope="scope">
          <div class="fengcai"><span>标题：</span>{{ scope.row.title }}</div>
          <div class="fengcai" v-if="scope.row.baseUser">
            <span>用户：</span>{{ scope.row.baseUser.realname }}
          </div>
          <div class="fengcai" v-if="scope.row.category">
            <span>资讯栏目：</span>{{ scope.row.category.name }}
          </div>
          <div class="fengcai">
            <span>发布时间：</span
            >{{ parseTime(scope.row.publishTime, "{y}-{m}-{d}") }}
          </div>
        </template>
      </el-table-column>
      <!-- 状态 -->
      <el-table-column align="center" prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag :type="scope.row.type">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="recommendById(scope.row.id)"
            class="btn"
            type="text"
            size="mini"
            >推荐</el-button
          >
          <el-button
            @click="checkById(scope.row.id)"
            class="btn"
            type="text"
            size="mini"
            >审核</el-button
          >
          <el-button
            @click="deleteById(scope.row.id)"
            class="btn"
            type="text"
            size="mini"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 分页开始 -->
    <el-pagination
      style="float: right; margin: 10px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="list.page"
      :page-sizes="[total, 12, 9, 6, 3]"
      :page-size="list.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 分页结束 -->
    <!-- 审核对话框开始 -->
    <el-dialog title="审核风采信息" :visible.sync="dialogVisible" width="60%">
      <div style="text-align: center; font-size: 20px; width: 100%">
        {{ check.title }}
      </div>
      <div v-html="check.content"></div>
      <span slot="footer" class="dialog-footer">
        <el-button type="danger" @click="changeStatus('审核不通过')"
          >审核不通过</el-button
        >
        <el-button type="primary" @click="changeStatus('审核通过')"
          >审核通过</el-button
        >
      </span>
    </el-dialog>
    <!-- 审核对话框结束 -->
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
import { parseTime } from "../../utils/index";
import { get, post } from "../../utils/request";
export default {
  data() {
    return {
      // status: ["未审核", "审核通过", "审核不通过", "推荐"],
      statusValue: "",
      categoryValue: "",
      title: "",
      list: {
        page: 1,
        pageSize: 100,
      },
      orders: null,
      dialogVisible: false,
      check: {},
      checkId: 0,
    };
  },
  computed: {
    ...mapState("webInfo", ["info", "total", "category","status"]),
  },
  methods: {
    // 转换时间
    parseTime,
    // 选择显示xxx条/页
    handleSizeChange(val) {
      this.list.pageSize = val;
      this.findAllInfo(this.list);
    },
    // 显示当前页
    handleCurrentChange(val) {
      this.list.page = val;
      this.findAllInfo(this.list);
    },
    ...mapActions("webInfo", ["findAllInfo", "findAllCategory"]),

    // 查询
    search() {
      let data = {
        ...this.list,
        title: this.title,
        status: this.statusValue,
        categoryId: this.categoryValue,
      };
      console.log(data);
      this.findAllInfo(data);
    },
    // 跳转发布风采
    toPublish() {
      this.$router.push("/webInfo/infoStudent");
    },
    // 改变状态
    changeStatus(status) {
      this.$confirm("你确定要进行此操作吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          post("/article/changeStatus", {
            id: this.checkId,
            status: status,
          }).then((res) => {
            this.dialogVisible = false;
            this.findAllInfo(this.list);
            this.$message({
              type: "success",
              message: "操作成功",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作",
          });
        });
    },
    // 推荐
    recommendById(id) {
      this.checkId = id;
      this.changeStatus("推荐");
    },
    // 审核
    checkById(id) {
      this.checkId = id;
      get("/article/findById", { id: id }).then((res) => {
        this.check = res.data;
        this.dialogVisible = true;
      });
    },
    // 删除
    deleteById(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          get("/article/deleteById", { id }).then((res) => {
            this.$message({
              type: "success",
              message: res.message,
            });
            this.findAllInfo(this.list);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.findAllCategory();
    this.findAllInfo(this.list);
  },
};
</script>
<style lang="scss" scoped>
.el {
  width: 200px;
  display: inline-block;
  margin: 10px;
}
.fengcai {
  margin: 5px;
}
span {
  font-weight: bold;
}
.btn:nth-child(1) {
  color: orange;
}
.btn:nth-child(2) {
  color: rgb(82, 206, 117);
}
.btn:nth-child(3) {
  color: red;
}
.el-dialog {
  background-color: rgb(44, 181, 172);
  color: white;
}
</style>